<!DOCTYPE html>
<html lang="zh" style="background-color: #fff;" >
<head>
	<meta charset="utf-8" />
	<title>业务目录</title> 
	<link href="${mvcPath}/dacp-lib/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" media="screen"/>	
  	<link href="${mvcPath}/dacp-view/aijs/css/ai.css" type="text/css" rel="stylesheet" />
  	<link href="${mvcPath}/dacp-res/catalog/common/css/ai.meta.css" type="text/css" rel="stylesheet"/>
	<link href="${mvcPath}/dacp-lib/fuelux/fuelux.min.css" type="text/css" rel="stylesheet" media="screen"/>
<script type="text/javascript" src="${mvcPath}/dacp-lib/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/underscore/underscore-min.js"></script>
<script src="${mvcPath}/dacp-lib/fuelux/fuelux.min.js"></script>
<script src="${mvcPath}/dacp-lib/cryptojs/aes.js" type="text/javascript"></script>
<script src="${mvcPath}/crypto/crypto-context.js" type="text/javascript"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.core.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.field.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.jsonstore.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.grid.ux.js"></script>
<style>
</style>
<script>
var catastore;
var cataList;
var reset = function(val){
	var id = $(val).attr("id");
	var state = id.substring(0,id.indexOf("b")+1);
	var xmlid = id.substring(id.indexOf("b")+1);
	var rec = catastore.getRecordByKey(xmlid);
	if(state=="nopub"){
		rec.set("STATE","未发布");
		catastore.commit();
		cataList.reload();	
	}
	if(state=="pub"){
		rec.set("STATE","发布");
		catastore.commit();
		cataList.reload();
	}
};
$(document).ready(function() {
	var sql = "select CATALOG_CODE,CATALOG_NAME,CATALOG_TYPE,REMARK,USER_LIST,USER_ROLE,CREATER,CRT_DATE,STATE,ORDSEQ,XMLID,CATALOG_RIGHT from data_catalog_def";
	catastore = new AI.JsonStore({
		sql:sql,
		pageSize:20,
		table:"DATA_CATALOG_DEF",
		key:"XMLID"
	});
	var config = {
		items:[{
            type: 'text',
            label: '目录编码',
            notNull: 'N',
            fieldName: 'CATALOG_CODE'
        },{
            type: 'text',
            label: '目录名称',
            notNull: 'N',
            fieldName: 'CATALOG_NAME'
        },{
            type: 'combox',
            label: '目录类型',
            notNull: 'N',
            editable:'N',
            fieldName: 'CATALOG_RIGHT',
            storesql:'公有,私有'
        },{
            type: 'combox',
            label: '用户角色',
            notNull: 'Y',
            editable:'N',
            fieldName: 'USER_ROLE',
            storesql:'select GROUPCODE,GROUPNAME from metagroup'
            //checkItems:'USER_LIST'
        },{
            type: 'selbox',
            label: '用户列表',
            notNull: 'Y',
            fieldName: 'USER_LIST',
            //storesql:"SELECT a.USERNAME values1,a.USECNNAME values2 FROM metauser a LEFT JOIN metagroupuser b on a.USERNAME=b.USERNAME WHERE b.GROUPCODE='{val}'"
            storesql:"select USERNAME values1,USECNNAME values2 from metauser"
        },{
            type: 'combox',
            label: '状态',
            notNull: 'Y',
            editable:'N',
            fieldName: 'STATE',
            storesql:'发布,未发布'
        },{
            type: 'text',
            label: '排序顺序',
            notNull: 'Y',
            fieldName: 'ORDSEQ'
        },{
            type: 'textarea',
            label: '描述',
            notNull: 'Y',
            fieldName: 'REMARK'
        }]
	};
	var deleteNotice = function(){
		var obj=$(".repeater-list tr.datarow.active")[0];
		if(!obj){
			alert('请选择一行记录!');
			return;
		}
		var rec = catastore.curRecord;
		var result = confirm("是否删除选择项?");
		if(result){
			catastore.remove(rec);
			catastore.commit();
			cataList.reload();
			alert("删除成功！");
		}
		
	};
	var queryNotice = function(){
		var qtitle = $("#q_notice_title").val();
		var newsql = "select CATALOG_CODE,CATALOG_NAME,CATALOG_TYPE,REMARK,USER_LIST,CREATER,CRT_DATE,STATE,ORDSEQ from data_catalog_def where CATALOG_CODE like '%"+qtitle+"%' or CATALOG_NAME like '%"+qtitle+"%'";
		catastore.select(newsql);
		cataList.reload();
	};
	var iscreat;
	var addNotice = function(){
		iscreat = true;
		//catastore.select();
		var rec = catastore.getNewRecord();
		//catastore.add(rec);
		rec.set("XMLID",ai.guid());
		rec.set("STATE","发布");
		catastore.curRecord = rec;
		ai.openFormDialog("新建记录",config.items,callBack,catastore);
	};
	var curcode;
	var updateNotice = function(){
		var obj=$(".repeater-list tr.datarow.active")[0];
		if(!obj){
			alert('请选择一行记录!');
			return;
		}
		//var rec = catastore.curRecord;
		//var key = rec.get("XMLID");
		//catastore.select();
		//catastore.curRecord = catastore.getRecordByKey(key);
		iscreat = false;
		curcode = catastore.curRecord.get("CATALOG_CODE");
		ai.openFormDialog("修改记录",config.items,callBack,catastore);
	};
	Array.prototype.contains = function ( needle ) {
	  for (i in this) {
	    if (this[i] == needle) return true;
	  }
	  return false;
	};
	var callBack = function(){
		var boo = true;
		var catalog_code = $("#CATALOG_CODE").val();
		var catalog_name = $("#CATALOG_NAME").val();
		var catalog_right = $("#CATALOG_RIGHT").val();
		var ordseq = $("#ORDSEQ").val();
		var sql = "select CATALOG_CODE from data_catalog_def";
		var codestore = ai.getStoreData(sql);
		var codearry = [];
		for(var i=0;i<codestore.length;i++){
			codearry.push(codestore[i]["CATALOG_CODE"]);
		}
		if(!catalog_code||!catalog_name||!catalog_right){
			boo = false;
			alert("请填写带*的项");
		}
		if(ordseq){
			if(!/^[1-9]\d{0,3}$/.test(ordseq)){
				boo = false;
				alert("排序顺序为应为1~9999的正整数！");
			}
		}
		if(iscreat){
			if(codearry.contains(catalog_code)){
				boo = false;
				alert("编码已被占用");
			}
		}
		if(!iscreat){
			if(curcode!==catalog_code){
				if(codearry.contains(catalog_code)){
					boo = false;
					alert("编码已被占用");
				}
			}
			
		}
		if(boo){
			if(iscreat){
				catastore.add(catastore.curRecord);
				catastore.commit();
				cataList.reload();
				alert("创建成功");
			}else{
				catastore.commit();
				cataList.reload();
				alert("更新成功");
			}
			
		}else{
			//$(".repeater-list tr.datarow.active").removeClass("active");
			cataList.reload();
			return boo;
		}
	};
	var config1 = {
		containerId:"cataloginfo",
		title:"业务目录",
		store:catastore,
		show:true,
		showcheck:false, 
		viewtype:"list",
		card:{
 			tpl:'<section id="{{CATALOG_CODE}}" class="dacptiles  panel panel-default">'
 				+'<header class="panel-heading"><small>{{CATALOG_NAME}}</small></header>'
 				+'<div class="panel-body"></div>'
 				+'<div class="panel-footer">'
 				+'	</div></section>'
 		},
		columns:[
			{header: "目录编码", dataIndex: "CATALOG_CODE", headerAlign: "center",width: "200",sortable:true},
			{header: "目录名称", dataIndex: "CATALOG_NAME", headerAlign: "center",width: "200",sortable:true},
			{header: "目录类型", dataIndex: "CATALOG_RIGHT", headerAlign: "center", width: "100",sortable:true},
			{header: "用户列表", dataIndex: "USER_LIST", headerAlign: "center",width: "100",sortable:true},
			{header: "创建人", dataIndex: "CREATER", headerAlign: "center",width: "200",sortable:true},
			{header: "创建时间", dataIndex: "CRT_DATE", headerAlign: "center",width: "200",sortable:true},
			{header: "状态", dataIndex: "STATE", headerAlign: "center",width: "100",sortable:true,},
			{header: "操作", dataIndex: "XMLID", headerAlign: "center",width: "150",sortable:true,render:function(a,b){if(a.STATE=='发布'){return '<button class="btn btn-info  btn-xs " id="nopub'+a.XMLID+'" onclick="reset(this);">取消发布</button>'}else{return '<button class="btn btn-info  btn-xs " id="pub'+a.XMLID+'" onclick="reset(this);">发布</button>'}}}
		],
		toolbarconfig:[
   			{type:'text',fieldLabel:'标题',width:120,name:'q_notice_title'}
   			,{type:'button',fieldLabel:'查询',id:'queryNotice',width:120,fn:queryNotice}
   			,{type:'button',fieldLabel:'新建',id:'addNotice',width:120,fn:addNotice}
   			,{type:'button',fieldLabel:'修改',id:'updateNotice',width:120,fn:updateNotice}
   			,{type:'button',fieldLabel:'删除',id:'updateNotice',width:120,fn:deleteNotice}
     	]
	};
	cataList = new AI.GridUx(config1);
	cataList.on('dblclick',function(){
		var catalog = catastore.curRecord.get("CATALOG_CODE");
		//if(MainSheet){
		//	MainSheet.addTabSheet("12wq","跳转", "/me/ftl/meta_database/ai.meta.dbstruct?dbName="+catalog,true,true);
		//}else{
		window.location.href="/"+contextPath+"/ftl/catalog/dataindex/catalogMgrDetail?catalogCode="+catalog;
		//}
	});
	var showErrorTip = function(fieldName,errTip){
   		var fieldElement = $("#"+fieldName);    	 
		var tip='<a href="#" data-toggle="tooltip" style="color:red" title="'+errTip+'"><span class="glyphicon glyphicon-info-sign"></span></a>';
		fieldElement.after(tip);
	};
});
</script>
</head>
<body  style="height:100%">
	<div class="frame-main" style="top:0px;position: absolute;left: 0px;right: 0px;bottom:0px;overflow:hidden">
       <div id="cataloginfo"  style="height:100%;"></div>
	</div>
</body>
</html>